package com.jcb.countfrench.view;




import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcb.countfrench.MainActivity;
import com.jcb.countfrench.R;
import com.jcb.countfrench.model.Quizz;
import com.jcb.countfrench.model.QuizzQuestionReponse;
import com.jcb.countfrench.model.User;
import com.jcb.countfrench.presenter.CountPresenter;
import com.jcb.countfrench.presenter.QuizzPresenter;

public class QuizzView extends LinearLayout {	
	Button btnChoiceQuizz ;
	QuizzPresenter quizzPresenter ;
	final MainActivity mainActivity ;
	QuizzQuestionReponse currentData ;
	MediaPlayer mediaPlayer ;
	Button button01, button02, button03, button04, buttonRepeat  ;
	TextView textViewInfo, textViewScore ;
	//int idQuizz  ;
	Quizz quizz ;
	
	public QuizzView(Context context, final MainActivity mainActivity, User user, CountPresenter presenter) {
		super(context);
		this.mainActivity = mainActivity ;
		LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(R.layout.quizzview,this);
		
		
		button01  = (Button) view.findViewById(R.id.btnQuizzChoice1);
		button02  = (Button) view.findViewById(R.id.btnQuizzChoice2);
		button03  = (Button) view.findViewById(R.id.btnQuizzChoice3);
		button04  = (Button) view.findViewById(R.id.btnQuizzChoice4);
		buttonRepeat  = (Button) view.findViewById(R.id.btnQuizzRepeat);
		textViewInfo = (TextView) view.findViewById(R.id.textInfoQuizz);
		textViewScore = (TextView) view.findViewById(R.id.textScore);
		
		buttonRepeat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("WXW", "repeat" + Thread.currentThread().getName());
				display ( currentData);
			}
		}
		) ;
		
		button01.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("WXW", "clic btn choice quizz" + Thread.currentThread().getName());
				receiveUserAnswer(button01.getText().toString(),  currentData);
			}			
		}
		) ;
		
		button02.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("WXW", "clic btn choice quizz" + Thread.currentThread().getName());
				receiveUserAnswer(button02.getText().toString(),  currentData);
			}			
		}
		) ;

		button03.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("WXW", "clic btn choice quizz" + Thread.currentThread().getName());
				receiveUserAnswer(button03.getText().toString(),  currentData);
			}			
		}
		) ;

		button04.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("WXW", "clic btn choice quizz" + Thread.currentThread().getName());
				receiveUserAnswer(button04.getText().toString(),  currentData);
			}			
		}
		) ;

	}
	
	public void receiveUserAnswer(String solutionUser,QuizzQuestionReponse quizzQuestionReponse) {
		//if (quizzQuestionReponse != null) {
			//Log.i("WXW", " solutionUser " + solutionUser + " getSolution()" + quizzQuestionReponse.getSolution());
			quizzPresenter.traiteReponseUser(solutionUser, quizz, quizzQuestionReponse);
			textViewScore.setText(quizz.getNbReponseOK() + " / " + quizz.getNumQuizzQuestionReponse());
			textViewInfo.setText(quizz.getInfoQuestion());
			currentData = quizzPresenter.nextItem(quizz);
	        display(currentData);
	}
	
	
	public MediaPlayer getMediaPlayer() {
		return mediaPlayer;
	}

	public void setMediaPlayer(MediaPlayer mediaPlayer) {
		this.mediaPlayer = mediaPlayer;
	}

	public QuizzPresenter getQuizzPresenter() {
		return quizzPresenter;
	}

	public void setQuizzPresenter(QuizzPresenter quizzPresenter) {
		this.quizzPresenter = quizzPresenter ;
	}

	public void initialDisplay(Quizz quizzCourant) {
		this.quizz = quizzCourant ;
		currentData = quizzPresenter.nextItem(quizzCourant);
		display(currentData) ;
	}
	
	public void display(QuizzQuestionReponse quizzQuestionReponse) {		
		int sound = 0 ;
		
		if (quizzQuestionReponse != null) {
			button01.setText(quizzQuestionReponse.getListReponses().get(0)) ;
			button02.setText(quizzQuestionReponse.getListReponses().get(1)) ;
			button03.setText(quizzQuestionReponse.getListReponses().get(2)) ;
			button04.setText(quizzQuestionReponse.getListReponses().get(3)) ;
			
			sound = quizzQuestionReponse.getSoundLangRef();
			Log.i("WXW", " lib lang " + quizzQuestionReponse.getSolution() + " sound=" + sound);
		}
	
		if (sound > 0) {
			mediaPlayer.reset();
			mediaPlayer = MediaPlayer.create(this.getContext(), quizzQuestionReponse.getSoundLangRef());
			mediaPlayer.start();
		}		
	}

	
	



	
}


